package br.ufpe.cin.rgms.linha.apresentacao;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import br.ufpe.cin.rgms.Facade;
import br.ufpe.cin.rgms.base.RGMSException;
import br.ufpe.cin.rgms.linha.modelo.Linha;
import br.ufpe.cin.rgms.util.Properties;

public class AlterarDadosLinhaServlet extends HttpServlet {

	private HashMap<String, String> formfields;
	private byte[] pdfFile;

	@Override
	public void init() throws ServletException {
		super.init();

		this.formfields = new HashMap<String, String>();
	}

	private void processFormField(FileItem item) {
		// Process a regular form field
		if (item.isFormField()) {
			String name = item.getFieldName();
			String value = item.getString();

			this.formfields.put(name, value);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		ServletContext servletContext = getServletContext();

		FileItemFactory factory = new DiskFileItemFactory();
		
		ServletFileUpload upload = new ServletFileUpload(factory);

		try {
			List /* FileItem */items = upload.parseRequest(request);

			// Process the uploaded items
			Iterator iter = items.iterator();

			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {
					processFormField(item);
				}
			}

			String idLinha = this.formfields.get("idLinha");

			String nome = this.formfields.get("nome");

			String sumario = this.formfields.get("sumario");
			
			String descricao = this.formfields.get("descricao");

			Linha linhaParaAlterar = Facade.getInstance().getLinha(Integer.parseInt(idLinha));

			linhaParaAlterar.setNome(nome);
			linhaParaAlterar.setSumario(sumario);
			linhaParaAlterar.setDescricao(descricao);

			Facade.getInstance().alterarLinha(linhaParaAlterar);

			request.setAttribute("linhastatus", Properties.getProperty(servletContext, "linha_alt_suc"));
		} catch (RGMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			request.setAttribute("linhastatus", Properties.getProperty(servletContext, "erro_upload"));
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		RequestDispatcher view = request.getRequestDispatcher("linhastatus.jsp");
		view.forward(request, response);
	}

}
